<?php
/*
 * @Author: 1086 cchcc@qq.com
 * @Date: 2024-04-22 10:38:36
 * @LastEditors: 1086 cchcc@qq.com
 * @LastEditTime: 2024-05-26 21:10:03
 * @FilePath: \erp\api\php\app\admin_v1\middleware\Login.php
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
namespace app\admin_v1\middleware;

// 登录检查中间件
class Login
{
    public function handle($request, \Closure $next)
    {
        // 请求方式验证
        if (!request()->isPost()) {
            return json(["code" => 201, "msg" => "Please use POST mode",],404);
        }
        // 非login控制器页面处理：
        if (!preg_match("/login/", $request->pathinfo())) {
            /** 缓存文件名=md5(md5(APP应用名+访问者IP)+$header_token) */
            $user_cache_name = md5(md5(app("http")->getName() . request()->ip()) . request()->header("token"));

            // $request222 = [
            //     "1获取当前应用名" => app("http")->getName(),
            //     "2TP版本" => \think\facade\App::version(),
            //     "3PHP版本号" => phpversion(),
            //     "4获取当前请求的时间" => request()->time(),
            //     "5当前的请求类型" => request()->method(),
            //     "6当前请求的资源类型" => request()->type(),
            //     "7当前访问协议" => $request->scheme(),
            //     "8当前访问的端口" => $request->port(),
            //     "9当前请求的REMOTE_PORT" => $request->remotePort(),
            //     "10当前请求的SERVER_PROTOCOL" => $request->protocol(),
            //     "11当前请求的CONTENT_TYPE" => $request->contentType(),

            //     "12获取客户端IP地址" => request()->ip(),
            //     "13检测是否是合法的IP地址" => request()->isValidIP(request()->ip()),
            //     "14将IP地址转换为二进制字符串" => request()->ip2bin(request()->ip()),

            //     "15检测是否使用手机访问" => request()->isMobile(),
            //     "16获取当前包含协议的域名" => request()->domain(),
            //     "17获取当前根域名" => request()->rootDomain(),
            //     "18获取当前子域名" => request()->subDomain(),
            //     "19获取当前泛域名的值" => request()->panDomain(),
            //     "20获取当前完整URL 包括QUERY_STRING" => request()->url(),
            //     "21获取当前URL 不含QUERY_STRING" => request()->baseUrl(),
            //     "22获取当前执行的文件 SCRIPT_NAME" => request()->baseFile(),
            //     "23获取URL访问根地址" => request()->root(),
            //     "24获取URL访问根目录" => request()->rootUrl(),
            //     "25获取当前请求URL的pathinfo信息 含URL后缀" => request()->pathinfo(),
            //     "26当前URL的访问后缀" => request()->ext(),
            //     "27当前host" => request()->host(),
            //     "28当前是否ssl" => request()->isSsl(),
            //     "29当前是否JSON请求" => request()->isJson(),
            //     "30当前是否Ajax请求" => request()->isAjax(),
            //     "31当前是否Pjax请求" => request()->isPjax(),
            //     "32当前请求参数" => request()->param(),
            //     "33设置或者获取当前的Header" => request()->header(),
            //     "34获取server参数" => request()->server(),
            //     "获取当前控制器：" => request()->controller(),
            //     "获取当前方法名：" => request()->action(),
            //     "_环境变量" => env(),//https://doc.thinkphp.cn/v8_0/helper.html
            //     "__header_token" => request()->header("token"),
            //     "__cache_name" => $user_cache_name,
            //     "__" . $user_cache_name => cache($user_cache_name)

            // ];
            // return json($request222);

            // 请求头Token假
            if (empty(request()->header("token"))) {
                return json(["code" => 909, "msg" => "请登录", "msg2" => "不合规:请求头的Token",]);
            }

            if (empty(cache($user_cache_name))) {
                return json(["code" => 909, "msg" => "请登录", "msg2" => "不合规:后台缓存文件不存在",]);
            } else {
                // return json(["code" => 202, "msg" => "当前用户登录缓存", "cache" => cache($user_cache_name)]);
            }
        }

        // 前置中间件
        $response = $next($request);
        // 后置中间件
        return $response;
    }
    // 中间件结束调度
    public function end(\think\Response $response)
    {
    }
    // 点击量
    public function read()
    {
        // $id = input("param.id");
        // $userId = 33;
        // Cache::inc("clickNum" . $id);
        // $clickNum = Cache::get("clickNum" . $id);
        // $NewsModel = new NewsModel();
        // $info = $NewsModel->getNewsById($id);
        // $info["data"]["clickNum"] = $clickNum;
        // return json($info);
    }
}
